package com.dcrzl.shcz.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.dcrzl.shcz.common.service.BaseService;
import com.dcrzl.shcz.common.utils.*;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

public abstract class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> implements BaseService<T> {
    @Autowired
    protected M baseMapper;

    @Override
    public Result<Integer> insert(T entity) {
        return ResultUtils.insertResult(baseMapper.insert(entity));
    }

    @Override
    public Result<Integer> insertBatch(Collection<T> entityList) {
        return ResultUtils.insertResult(baseMapper.insertBatchSomeColumn(entityList));
    }

    @Override
    public Result<Integer> updateById(T entity) {
        return ResultUtils.updateResult(baseMapper.updateById(entity));
    }

    @Override
    public Result<Integer> update(T entity, Wrapper<T> wrapper) {
        return ResultUtils.updateResult(baseMapper.update(entity,wrapper));
    }

    @Override
    public Result<T> selectById(Serializable id) {
        return ResultUtils.selectOneResult(baseMapper.selectById(id));
    }

    @Override
    public Result<T> selectOne(Wrapper<T> wrapper) {
        return ResultUtils.selectOneResult(baseMapper.selectOne(wrapper));
    }

    @Override
    public Result<List<T>> selectList(Wrapper<T> wrapper) {
        return ResultUtils.selectResult(baseMapper.selectList(wrapper));
    }

    @Override
    public PageInfo<T> selectPage(BaseEntity baseEntity, Wrapper<T> wrapper) {
        return PageUtils.startPage(baseMapper.selectPage(PageUtils.getPageParam(baseEntity),wrapper));
    }

    @Override
    public Result<List<T>> selectBatchIds(Collection<Serializable> list) {
        return ResultUtils.selectResult(baseMapper.selectBatchIds(list));
    }

    @Override
    public Result<Integer> deleteById(Serializable id) {
        return ResultUtils.deleteResult(baseMapper.deleteById(id));
    }


    @Override
    public Result<Integer> deleteBatchIds(Collection<? extends Serializable> idList) {
        return ResultUtils.deleteResult(baseMapper.deleteBatchIds(idList));
    }
}
